home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / std / c++ / 482 < prev    next >
Encoding:
Internet Message Format  |  1996-08-06  |  4.7 KB

  1. Path: engnews1.Eng.Sun.COM!taumet!clamage
  2. From: robert.davies@vuw.ac.nz (Robert Davies)
  3. Newsgroups: comp.std.c++
  4. Subject: Standard string class
  5. Date: 18 Feb 1996 02:14:33 GMT
  6. Organization: ?
  7. Approved: clamage@eng.sun.com (comp.std.c++)
  8. Message-ID: <199602171134.LAA00305@kauri.vuw.ac.nz>
  9. NNTP-Posting-Host: taumet.eng.sun.com
  10. Mime-Version: 1.0
  11. Content-Type: text/plain; charset="us-ascii"
  12. X-Sender: robertd@kauri.vuw.ac.nz (Unverified)
  13. X-Mailer: Windows Eudora Version 1.4.4
  14. Content-Length: 3925
  15. Originator: clamage@taumet
  16.  
  17. Here are some comments and a query about the standard string class. I am
  18. working from the January 96 working papers, but the comments also apply to
  19. the April 95 version of the standard.
  20.  
  21.  
  22. Reallocation
  23. ------------
  24.  
  25. Under the section on reserve the draft standard says that no reallocation
  26. of the storage takes place during insertions until the string reaches
  27. the length given by the last call of reserve. The standard does not say
  28. what insertion means. I assume it means append, +=, and possibly insert
  29. statements. Is this correct? If so the value returned by data() should
  30. remain valid following a call of append, += or insert if the string doesn't
  31. exceed the length set by reserve.
  32.  
  33. But this is conflict with the description of data() which says the value
  34. returned becomes invalid after any non-constant call to the string concerned.
  35.  
  36. Am I missing something or does this still need to be worked out?
  37.  
  38. ...recommendation: this be clarified
  39.  
  40.  
  41. reserve
  42. -------
  43.  
  44. The reserve function allows you to increase the capacity but there doesn't
  45. seem to be anyway of decreasing it. So before working of a string we might
  46. increase the capacity to something large to avoid repeated reallocating
  47. storage. When we have finished we might want to set the capacity down to size()
  48. to save space. There doesn't seem to be anyway of doing this.
  49.  
  50. ...recommendation: a command be included to reduce the capacity to the
  51. length of the string, eg reserve().
  52.  
  53.  
  54. at and operator[]
  55. -----------------
  56.  
  57. The same comments apply here as applied to data() in my comments on
  58. reallocation.
  59.  
  60. Are we to assume that
  61. x[3] = 'a';
  62. as an example, may cause a reallocation, and so invalidate data()?
  63.  
  64. at and operator[] seem almost identical except that operator[] isn't required
  65. to check bounds. However, the const version of x[x.size()] is supposed to
  66. return traits::eos(), so if we store our strings without a trailing
  67. traits::eos() we will have to check bounds. Is this is what is intended?
  68.  
  69. The constant version of operator[] returns a charT whereas the constant
  70. version of at returns a const_reference. What is the point of this 
  71. difference and what effect does it have?
  72.  
  73. ...recommendation: remove the requirement that x[x.size()] return
  74. traits::eos() and clarify the difference between operator[] and
  75. at.
  76.  
  77. Question: are we safe to assume that something = x[3] will use the constant
  78. version of the operator?
  79.  
  80.  
  81. c_str
  82. -----
  83.  
  84. Same comments apply as applied to data() in the section on reallocation.
  85.  
  86.  
  87. data
  88. ----
  89.  
  90. Also note that the value might be destroyed by a call to c_str(), since we
  91. might have to reallocate the string to accommodate a trailing traits::eos().
  92.  
  93. ... recommendation: under the section of data() note that the returned value
  94. may be invalidated by a call to c_str.
  95.  
  96.  
  97. reallocation again
  98. ------------------
  99.  
  100. Suppose I am right in thinking we can update a string with operator[], at,
  101. insert or append without causing reallocation. Suppose also we are writing
  102. a string package with copy-on-write. Then copy-on-write must be disabled on
  103. string for which data() etc has been called. We need some way of telling a
  104. program that copy-on-write can be reactivated - eg we are no longer interested
  105. in the value returned by data(). Calling reserve() as I have defined above
  106. may be one possibility.
  107.  
  108. ... recommendation: some way be found for allowing a user to re-instate
  109. copy-on-write.
  110.  
  111.  
  112. find etc.
  113. --------
  114.  
  115. Most of the functions that deal with two strings (such as replace and
  116. compare in the January papers) use the order of parameters:
  117.    pos and n for the string on the left of the '.'
  118.    the name of the second string
  119.    pos and n for the second string.
  120.  
  121. But find does not follow this convention. Here pos follows the second string
  122. but refers to the string on the left of '.' . The situation is
  123. doubly confusing for str.find(s, pos ,n) where s is a charT*. Here
  124. pos refers to str and n refers to s.
  125.  
  126. ...recommendation: fix this.
  127.  
  128.  
  129. That's all for now.
  130.  
  131. Robert
  132.  
  133.  
  134.  
  135. [ To submit articles: Try just posting with your newsreader.  If that fails,
  136.               use mailto:std-c++@ncar.ucar.edu
  137.   FAQ:    http://reality.sgi.com/employees/austern_mti/std-c++/faq.html
  138.   Policy: http://reality.sgi.com/employees/austern_mti/std-c++/policy.html
  139.   Comments? mailto:std-c++-request@ncar.ucar.edu
  140. ]
  141.